
from whiff import resolver
from whiff.middleware import displayTraceback
import fileSystemResource

env = {
    'treeView.bodyPrefix': "<div style=\"border-left:3px solid green; padding-left:10px\">",
    'treeView.bodySuffix': "</div>",
    'treeView.tailPrefix': "<div style=\"border-left:3px solid yellow; padding-left:10px\">",
    'treeView.tailSuffix': "</div>"
    }

def directoryTreeApplication(rootDirectory):
    import showDir
    app = resolver.moduleRootApplication("/", showDir,
                                             #exception_middleware=None,
                                             exception_middleware=displayTraceback.__middleware__,
                                             on_not_found=None, # show traceback (could comment)
                                             environment = env,
                                             )
    tree = fileSystemResource.DirectoryTreeDictionary(rootDirectory)
    app.registerStaticResource(prefix="tree",
                               resourceValue=tree)
    app.registerStaticResource(prefix="root",
                               resourceValue=rootDirectory)
    return app

if __name__=="__main__":
    import wsgiref.simple_server
    import sys
    import os
    rootdir = ".."
    if len(sys.argv)>1:
        rootdir = sys.argv[1]
    rootdir = os.path.abspath(rootdir)
    testapp = directoryTreeApplication(rootdir)
    print "directory tree server"
    print "serving wsgi at 8888"
    srv = wsgiref.simple_server.make_server('localhost', 8888, testapp)
    srv.serve_forever()
                    
